package com.jfplugin.mail;

import com.jfplugin.mail.core.JavaMailSender;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发送邮件工具
 *
 * @author farmer
 */
public class MailKit {

    static final Map<String, MailPro> PRO_MAP = new HashMap<>();

    private static MailPro mailPro = null;

    static void init(String configName, MailPro mailPro) {
        if (PRO_MAP.get(configName) != null) {
            throw new RuntimeException(configName + "配置的Mail已经存在！");
        }
        PRO_MAP.put(configName, mailPro);
        if (MailPlugin.MAIN_CONFIG.equals(configName)) {
            MailKit.mailPro = mailPro;
        }
    }

    public static MailPro use(String configName) {
        MailPro mailPro = PRO_MAP.get(configName);
        if (mailPro == null) {
            throw new RuntimeException(configName + "配置的Mail不存在！");
        }
        return mailPro;
    }

    /**
     * 发送邮件
     *
     * @param to      收件人
     * @param cc      发件人
     * @param subject 主题
     * @param text    内容
     */
    public static void send(String to, List<String> cc, String subject, String text) {
        mailPro.send(to, cc, subject, text);
    }

    /**
     * 发送邮件
     *
     * @param to          收件人
     * @param cc          发件人
     * @param subject     主题
     * @param text        内容
     * @param attachments 附件
     */
    public static void send(String to, List<String> cc, String subject, String text, List<File> attachments) {
        mailPro.send(to, cc, subject, text, attachments);
    }

    public static void send(String to, List<String> cc, String subject, String viewpath, Map<String, Object> dataMap) {
        mailPro.send(to, cc, subject, viewpath, dataMap);
    }

    public static void send(String to, List<String> cc, String subject, String viewpath, Map<String, Object> dataMap, List<File> attachments) {
        mailPro.send(to, cc, subject, viewpath, dataMap, attachments);
    }

    /**
     * 获取JavaMailSender
     *
     * @return 当前实例的JavaMailSender，用来定制化更复杂的邮件发送需求
     */
    public static JavaMailSender getMailSender() {
        return mailPro.getMailSender();
    }

}
